// Neo4j 知识图谱初始化脚本

// 创建约束和索引
CREATE CONSTRAINT entity_id_unique IF NOT EXISTS FOR (e:Entity) REQUIRE e.id IS UNIQUE;
CREATE CONSTRAINT entity_name_graph_unique IF NOT EXISTS FOR (e:Entity) REQUIRE (e.name, e.graphId) IS UNIQUE;

// 创建索引以提高查询性能
CREATE INDEX entity_name_index IF NOT EXISTS FOR (e:Entity) ON (e.name);
CREATE INDEX entity_type_index IF NOT EXISTS FOR (e:Entity) ON (e.type);
CREATE INDEX entity_graph_index IF NOT EXISTS FOR (e:Entity) ON (e.graphId);
CREATE INDEX entity_creator_index IF NOT EXISTS FOR (e:Entity) ON (e.creator);
CREATE INDEX entity_create_time_index IF NOT EXISTS FOR (e:Entity) ON (e.createTime);

// 关系索引
CREATE INDEX relation_type_index IF NOT EXISTS FOR ()-[r:RELATES_TO]-() ON (r.type);
CREATE INDEX relation_graph_index IF NOT EXISTS FOR ()-[r:RELATES_TO]-() ON (r.graphId);
CREATE INDEX relation_strength_index IF NOT EXISTS FOR ()-[r:RELATES_TO]-() ON (r.strength);
CREATE INDEX relation_confidence_index IF NOT EXISTS FOR ()-[r:RELATES_TO]-() ON (r.confidence);

// 创建示例知识图谱数据

// 人工智能领域知识图谱
CREATE (ai:Entity {
    name: '人工智能',
    type: 'concept',
    description: '模拟人类智能的科学技术',
    confidence: 0.95,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {category: '技术领域', importance: '核心'}
});

CREATE (ml:Entity {
    name: '机器学习',
    type: 'concept',
    description: '算法自动学习的方法',
    confidence: 0.92,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {category: '技术分支', difficulty: '中等'}
});

CREATE (dl:Entity {
    name: '深度学习',
    type: 'concept',
    description: '基于神经网络的机器学习方法',
    confidence: 0.90,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {category: '技术分支', difficulty: '高'}
});

CREATE (pytorch:Entity {
    name: 'PyTorch',
    type: 'tool',
    description: '深度学习框架',
    confidence: 0.88,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {category: '开发工具', language: 'Python'}
});

CREATE (tensorflow:Entity {
    name: 'TensorFlow',
    type: 'tool',
    description: '机器学习平台',
    confidence: 0.90,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {category: '开发工具', language: 'Python'}
});

CREATE (zhangsan:Entity {
    name: '张三',
    type: 'person',
    description: '人工智能研究员',
    confidence: 0.85,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {position: '研究员', department: '技术部'}
});

CREATE (tencent:Entity {
    name: '腾讯公司',
    type: 'organization',
    description: '互联网科技公司',
    confidence: 0.95,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {industry: '互联网', location: '深圳'}
});

// 创建关系
CREATE (ml)-[:RELATES_TO {
    type: 'part_of',
    description: '机器学习是人工智能的重要组成部分',
    strength: 0.95,
    confidence: 0.90,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {category: '包含关系', importance: '核心'}
}]->(ai);

CREATE (dl)-[:RELATES_TO {
    type: 'part_of',
    description: '深度学习是机器学习的一个分支',
    strength: 0.90,
    confidence: 0.88,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {category: '包含关系', importance: '重要'}
}]->(ml);

CREATE (zhangsan)-[:RELATES_TO {
    type: 'works_for',
    description: '张三在腾讯公司工作',
    strength: 0.90,
    confidence: 0.95,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {position: '研究员', startDate: '2020-01-01'}
}]->(tencent);

CREATE (zhangsan)-[:RELATES_TO {
    type: 'researches',
    description: '张三研究人工智能领域',
    strength: 0.85,
    confidence: 0.80,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {expertise: '机器学习', years: '5'}
}]->(ai);

CREATE (zhangsan)-[:RELATES_TO {
    type: 'uses',
    description: '张三使用PyTorch进行开发',
    strength: 0.80,
    confidence: 0.75,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {proficiency: '熟练', usage: '日常开发'}
}]->(pytorch);

CREATE (pytorch)-[:RELATES_TO {
    type: 'used_for',
    description: 'PyTorch用于深度学习开发',
    strength: 0.90,
    confidence: 0.85,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {purpose: '模型训练', efficiency: '高'}
}]->(dl);

CREATE (tensorflow)-[:RELATES_TO {
    type: 'used_for',
    description: 'TensorFlow用于机器学习开发',
    strength: 0.88,
    confidence: 0.82,
    graphId: 1,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {purpose: '模型部署', scalability: '高'}
}]->(ml);

// 前端技术知识图谱
CREATE (frontend:Entity {
    name: '前端开发',
    type: 'concept',
    description: '用户界面开发技术',
    confidence: 0.95,
    graphId: 2,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {category: '技术领域', complexity: '中等'}
});

CREATE (react:Entity {
    name: 'React',
    type: 'tool',
    description: 'JavaScript用户界面库',
    confidence: 0.92,
    graphId: 2,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {category: '框架', language: 'JavaScript'}
});

CREATE (vue:Entity {
    name: 'Vue.js',
    type: 'tool',
    description: '渐进式JavaScript框架',
    confidence: 0.90,
    graphId: 2,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {category: '框架', language: 'JavaScript'}
});

CREATE (lisi:Entity {
    name: '李四',
    type: 'person',
    description: '前端开发工程师',
    confidence: 0.85,
    graphId: 2,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {position: '高级工程师', experience: '3年'}
});

// 前端技术关系
CREATE (react)-[:RELATES_TO {
    type: 'used_for',
    description: 'React用于前端开发',
    strength: 0.90,
    confidence: 0.88,
    graphId: 2,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {purpose: 'UI构建', popularity: '高'}
}]->(frontend);

CREATE (vue)-[:RELATES_TO {
    type: 'used_for',
    description: 'Vue.js用于前端开发',
    strength: 0.88,
    confidence: 0.85,
    graphId: 2,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {purpose: 'UI构建', learning_curve: '平缓'}
}]->(frontend);

CREATE (lisi)-[:RELATES_TO {
    type: 'specializes_in',
    description: '李四专精前端开发',
    strength: 0.85,
    confidence: 0.80,
    graphId: 2,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {expertise_level: '高级', focus_area: 'React'}
}]->(frontend);

CREATE (lisi)-[:RELATES_TO {
    type: 'uses',
    description: '李四使用React开发',
    strength: 0.90,
    confidence: 0.85,
    graphId: 2,
    createTime: datetime(),
    updateTime: datetime(),
    creator: 'system',
    status: 'active',
    properties: {proficiency: '专家级', project_count: '10+'}
}]->(react);